package com.developtools.doc.markdown;

import lombok.Getter;
import lombok.Setter;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liujun
 */
public class Markdown {

    private StringBuffer content;
    private String lf = "\n";

    private boolean writeRealtime = false;

    public Markdown(){
        this("\n");
    }

    public Markdown(String lf){
        this.lf = lf;
        content = new StringBuffer();
    }

    public void h1(String h1){
        h(1,h1);
    }

    public void h2(String h2){
        h(2,h2);
    }

    public void h3(String h3){
        h(3,h3);
    }

    public void h4(String h4){
        h(4,h4);
    }

    public void h(int n, String hContent){
        for (int i = 0; i < n; i++) {
            content.append("#");
        }
        content.append(" ");
        content.append(hContent);
        content.append(lf);
    }

    public void table(Table table){
        List<String> headers = table.getHeaders();
        String headerLine = headers.stream()
                .map(str -> "| " + str)
                .collect(Collectors.joining());
        headerLine = headerLine + " |";
        content.append(headerLine);
        content.append(lf);

        String headerNext = headers.stream()
                .map(str -> "| ---- ")
                .collect(Collectors.joining());
        headerNext = headerNext + " |";
        content.append(headerNext);
        content.append(lf);

        List<Vector<String>> contents = table.getContent();
        contents.forEach(v -> {
            String contentLine = v.stream().map(s -> "| " + s).collect(Collectors.joining());
            contentLine = contentLine + " |";
            content.append(contentLine);
            content.append(lf);
        });
        content.append(lf);
    }

    public void write(File file){
        if (writeRealtime) {

        }else {
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new FileWriter(file));
                writer.write(content.toString());
                writer.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (Objects.isNull(writer)) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

    public void code(String lang, String text){
        content.append("```");
        content.append(lang);
        content.append(lf);
        content.append(text);
        content.append(lf);
        content.append("```");
        content.append(lf);
    }

    public void p(String path) {
        content.append(path);
        content.append(lf);
    }


    @Getter
    @Setter
    public static class Table{
        private List<String> headers = new ArrayList<>();
        private List<Vector<String>> content = new ArrayList<>();

        public void putContent(String... strings){
            Vector<String> vs = new Vector<>(Arrays.asList(strings));
            content.add(vs);
        }
    }
}
